package com.team24.hotel.controller.common;

import com.team24.hotel.domain.*;
import com.team24.hotel.service.CityService;
import com.team24.hotel.service.HotelService;
import com.team24.hotel.service.OrderService;
import com.team24.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private HotelService hotelService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/toPay")
    public String toPayPage(String orderId, Model model){
        Order order=orderService.findByPrimaryKey(orderId);
        Room room=roomService.findRoomByPrimaryKey(order.getRoomId());
        model.addAttribute("order",order);
        model.addAttribute("room",room);
        return "frontPay";
    }

    @RequestMapping("/toBack")
    public String toBackIndexPage(HttpSession session){
        if(session.getAttribute("user")!=null){
            return "backIndex";
        }else {
            return "404";
        }
    }

    @RequestMapping("/toFront")
    public String toFrontIndexPage(HttpSession session, Model model){
        List<Hotel> hotHotels=hotelService.listHotelAndImageHotSome(6);
        List<Hotel> recentHotels=hotelService.listHotelAndImageNewBook(6);
        model.addAttribute("hotHotels",addHotelsSign(hotHotels));
        model.addAttribute("recentHotels",addHotelsSign(recentHotels));
        return "frontIndex";
    }

    List<Hotel> addHotelsSign(List<Hotel> hotels){
        for (Hotel hotel:hotels
        ) {
            hotel.setHotelSign(0);//判断该酒店是否可以入住,0代表不可入住
            int roomsSign=0;//是否有空房，0代表无
            for (Room room:roomService.listRoom(hotel.getHotelId())
            ) {
                if (room.getRoomSign()==1) roomsSign++;
            }
            if(!roomService.listRoom(hotel.getHotelId()).isEmpty()|| roomsSign>0){
                hotel.setHotelSign(1);
            }
        }
        return hotels;
    }

}
